﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DrSoftsFrame.Servers;

namespace PreeRegisterDAL
{
    public class LabReportDAL
    {
        /// <summary>
        /// 查询检验报告列表
        /// </summary>
        /// <param name="cpatientcode"></param>
        /// <returns></returns>
        public static DataSet GetLabReportList(string cpatientcode)
        {
            try
            {
                //改为MzZyNo为查询条件查询，解决cardno前面有时带前缀查询不到的问题  liujia151224
                string sqlstr = string.Format(@"select ApplyNo,ReportTypeName,TestSample,OrderNo,cjgsj,MainID
                                                from sufferertest_basicinfo
                                                where MzZyNo='{0}'
                                                --and cjgsj between CONVERT(varchar(10),DATEADD(MM,-9,GETDATE()),120)+' 0:00:00' and CONVERT(varchar(10),GETDATE(),120)+' 23:59:59'
                                                order by cjgsj desc", cpatientcode);
                LabDb mydb = new LabDb();
                DataSet ds = mydb.ExecuteSelect(sqlstr);
                if (ds == null || ds.Tables.Count < 1)
                {
                    return null;
                }
                ds.Tables[0].TableName = "sufferertest_basicinfo";
                return ds;

            }
            catch (Exception err)
            {

                throw;
            }
        }

        /// <summary>
        /// 查询检验报告明细
        /// </summary>
        /// <param name="cpatientcode"></param>
        /// <returns></returns>
        public static DataSet GetLabReportDetail(string mainid)
        {
            try
            {
                string sqlstr = string.Format(@"--查询报告单基本信息
                                                select isnull(a.MainID,0) as MainID,isnull(a.StandardSampleNo,0) as StandardSampleNo,
                                                isnull(a.SectionOffice,'0') as SectionOffice,isnull(a.jbsj,0) as jbsj,isnull(b.Name,0) as Name,
                                                isnull(a.TestSample,0) as TestSample,isnull(a.BedNo,0) as BedNo,isnull(a.jysj,0) as jysj,isnull(a.sex,0) as sex,
                                                isnull(a.SampleStatus,0) as SampleStatus,isnull(a.kdr,0)as kdr ,isnull(a.cjgsj,0) as cjgsj,isnull(a.Age,0)+AgeUnit as AgeUnit,
                                                rtrim(ltrim(str(JyrNum)))+'_'+ltrim(rtrim(a.jyr)) as jyr,isnull(a.Diagnose,0) as Diagnose,isnull(a.cbsj,0) as cbsj,
                                                rtrim(ltrim(str(HdrNum)))+'_'+ltrim(rtrim(a.hdr)) as hdr,cbr,c.ReportFormat,a.ReportTypeName,a.MzZyNo,a.kdsj
                                                from dbo.sufferertest_basicinfo a with(nolock) 
                                                join Card_BasicInfo b with(nolock) on a.CardNo=b.CardNo
                                                join testclassinfo c on a.TestClassNum=c.num
                                                where a.MainID={0}

                                                --查询报告单项目明细信息
                                                select TestItemChi,TestResult,Unit,ConsultChar,TestResultSign,testmeans,mutualvalue  
                                                from SuffererTest_ResultInfo  with(nolock)
                                                where DetailID={0}", mainid);
                LabDb mydb = new LabDb();
                DataSet ds = mydb.ExecuteSelect(sqlstr);
                if (ds == null || ds.Tables.Count < 2||ds.Tables[0].Rows.Count<1)
                {
                    return null;
                }
                return ds;

            }
            catch (Exception err)
            {

                throw;
            }
        }
    }
}
